package com.transac.demo.service;

import com.transac.demo.mapper.Account;
import com.transac.demo.mapper.AccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class TransactionSupportsServiceImpl implements TransactionSupportsService {

    @Autowired
    AccountMapper accountMapper;
    @Autowired
    TransactionSupportsServiceImpl transactionSupportsService;

    @Override
    public void updateMoney1() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionSupportsService.updateJerryMoney1();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateJerryMoney1() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        throw new RuntimeException("error");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMoney2() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);

        transactionSupportsService.updateJerryMoney2();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateJerryMoney2() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        throw new RuntimeException("error");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMoney3() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);

        transactionSupportsService.updateJerryMoney3();
        throw new RuntimeException("error");
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateJerryMoney3() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMoney4() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionSupportsService.updateJerryMoney4();
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateJerryMoney4() {
        try {
            Account account = accountMapper.selectByPrimaryKey(3);
            account.setMoney(account.getMoney() + 100);
            accountMapper.updateByPrimaryKey(account);
            throw new RuntimeException("error");
        } catch (Exception e) {

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMoney5() {
        try {
            Account account = accountMapper.selectByPrimaryKey(1);
            account.setMoney(account.getMoney() + 100);
            accountMapper.updateByPrimaryKey(account);
            transactionSupportsService.updateJerryMoney5();
        } catch (Exception e) {

        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void updateJerryMoney5() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        throw new RuntimeException("error");
    }

}
